package mood.su.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.Date;

import mood.su.data.DayMood;
import mood.su.data.Mood;
import mood.su.data.MoodDataManager;
import mood.su.data.MoodType;
import mood.su.mood.R;
import mood.su.utils.DateUtil;

public class DayMoodListAdapter extends BaseAdapter {
    private DayMood mDayMood;
    private Context mContext;
    private LayoutInflater mInflater;

    public DayMoodListAdapter(Context context, Date date){
        mDayMood = MoodDataManager.getManager().get(date, true);
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDayMood.count();
    }

    @Override
    public Mood getItem(int position) {
        return mDayMood.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.time_events_grid_item, null);
            holder = new ViewHolder();

            holder.image = (ImageView) convertView.findViewById(R.id.tv_mood_image);
            holder.text = (TextView) convertView.findViewById(R.id.tv_time);
            holder.desc = (TextView) convertView.findViewById(R.id.tv_mood_desc);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        DateFormat df = DateFormat.getTimeInstance();
        holder.text.setText(DateUtil.friendlyTime(getItem(position).getTime()));

        if(getItem(position).getType() == MoodType.HAPPY){
            holder.image.setImageResource(R.drawable.happy24x24);
        }else{
            holder.image.setImageResource(R.drawable.sad24x24);
        }

        String moodDesc = getItem(position).getDesc();
        if(moodDesc == null){
            holder.desc.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final EditText editText = new EditText(mContext);
                    editText.setLines(4);

                    new AlertDialog.Builder(mContext)
                            .setTitle("描述一下心情")
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setView(editText)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener(){
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //System.out.println(editText.getText());
                                    getItem(position).setDesc(editText.getText().toString());
                                    DayMoodListAdapter.this.notifyDataSetChanged();
                                }
                            })
                            .setNegativeButton("取消", null)
                            .show();
                }
            });
        }else{
            holder.desc.setText(moodDesc);
        }

        return convertView;
    }

    public final class ViewHolder{
        public ImageView image;
        public TextView text;
        public TextView desc;
    }
}
